package org.bugjail.dataaccess.jdbc.objects;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.bugjail.data.TestCycle;
import org.bugjail.data.TestRound;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.dataaccess.jdbc.connection.QueryException;
import org.bugjail.dataaccess.jdbc.connection.QueryManager;
import org.bugjail.dataaccess.objects.TestCycleDAO;
import org.bugjail.dataaccess.objects.TestRoundDAO;

public class JDBCTestRoundDAO implements TestRoundDAO {

	private final String PROPERTIES_FILE = "/org/bugjail/dataaccess/jdbc/properties/testrounddao.properties";

	private final String PROPERTY_RETRIEVE_BY_TESTCYCLE = "retrieve.bytestcycle";
	private final String PROPERTY_RETRIEVE_BY_ID = "retrieve.byid";
	private final String PROPERTY_INSERT = "insert";
	private final String PROPERTY_UPDATE = "update";
	
	private TestRound extractTestRoundFromResultSet(ResultSet rs, QueryManager queryManager, TestCycle testCycle) throws SQLException {
		TestRound testRound = new TestRound();
		int id = rs.getInt("id");
		testRound.setId(id);

		if (testCycle == null) {
			TestCycleDAO testCycleDao = DAOFactory.getDAOFactory().getTestCycleDAO();
			testRound.setTestCycle(testCycleDao
					.retrieveById(rs.getInt("testcycle_id")));
		} else {
			testRound.setTestCycle(testCycle);
		}
		testRound.setName(rs.getString("name"));
		
		return testRound;
	}
	
	@Override
	public List<TestRound> retrieveTestRoundsOfTestCycle(TestCycle testCycle) {
		List<TestRound> testRounds = new LinkedList<TestRound>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
						.getPreparedStatement(PROPERTY_RETRIEVE_BY_TESTCYCLE);
				statement.setInt(1, testCycle.getId());
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					testRounds.add(extractTestRoundFromResultSet(rs,
							queryManager, testCycle));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return testRounds;
	}

	@Override
	public void create(TestRound element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
						.getPreparedStatement(PROPERTY_INSERT);
				statement.setString(1, element.getName());
				statement.setInt(2, element.getTestCycle().getId());
				statement.executeUpdate();

				QueryManager.commit(queryManager);

			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public void delete(TestRound element) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<TestRound> retrieveAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TestRound retrieveById(int id) {
		TestRound testRound = null;
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
						.getPreparedStatement(PROPERTY_RETRIEVE_BY_ID);
				statement.setInt(1, id);
				ResultSet rs = statement.executeQuery();
				if (rs.next()) {
					testRound = extractTestRoundFromResultSet(rs, queryManager, null);
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return testRound;
	}

	@Override
	public List<TestRound> retrieveByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(TestRound element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
						.getPreparedStatement(PROPERTY_UPDATE);
				statement.setString(1, element.getName());
				statement.setInt(2, element.getTestCycle().getId());
				statement.setInt(3, element.getId());
				statement.executeUpdate();

				QueryManager.commit(queryManager);

			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

}
